﻿package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Lvyoujingdian;
import com.server.LvyoujingdianServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class LvyoujingdianController {
	@Resource
	private LvyoujingdianServer lvyoujingdianService;


   
	@RequestMapping("addLvyoujingdian.do")
	public String addLvyoujingdian(HttpServletRequest request,Lvyoujingdian lvyoujingdian,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		lvyoujingdian.setAddtime(time.toString().substring(0, 19));
		lvyoujingdianService.add(lvyoujingdian);
		session.setAttribute("backxx", "添加成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		
		//session.setAttribute("backurl", "lvyoujingdianList.do");
		
		return "redirect:postback.jsp";
		//return "redirect:lvyoujingdianList.do";
		
		
		
	}
 
//	处理编辑
	@RequestMapping("doUpdateLvyoujingdian.do")
	public String doUpdateLvyoujingdian(int id,ModelMap map,Lvyoujingdian lvyoujingdian){
		lvyoujingdian=lvyoujingdianService.getById(id);
		map.put("lvyoujingdian", lvyoujingdian);
		return "lvyoujingdian_updt";
	}
	
	
	
	
	
//	后台详细
	@RequestMapping("lvyoujingdianDetail.do")
	public String lvyoujingdianDetail(int id,ModelMap map,Lvyoujingdian lvyoujingdian){
		lvyoujingdian=lvyoujingdianService.getById(id);
		map.put("lvyoujingdian", lvyoujingdian);
		return "lvyoujingdian_detail";
	}
//	前台详细
	@RequestMapping("lyjdDetail.do")
	public String lyjdDetail(int id,ModelMap map,Lvyoujingdian lvyoujingdian){
		lvyoujingdian=lvyoujingdianService.getById(id);
		map.put("lvyoujingdian", lvyoujingdian);
		return "lvyoujingdiandetail";
	}
//	
	@RequestMapping("updateLvyoujingdian.do")
	public String updateLvyoujingdian(int id,ModelMap map,Lvyoujingdian lvyoujingdian,HttpServletRequest request,HttpSession session){
		lvyoujingdianService.update(lvyoujingdian);
		session.setAttribute("backxx", "修改成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		return "redirect:postback.jsp";
		//String url = request.getHeader("Referer");
		//return "redirect:"+url;
		//return "redirect:lvyoujingdianList.do";
	}

//	分页查询
	@RequestMapping("lvyoujingdianList.do")
	public String lvyoujingdianList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyoujingdian lvyoujingdian, String jingdianmingcheng, String jingdiandengji, String suozaichengshi, String diliweizhi, String menpiaojiage1,String menpiaojiage2, String kaifangshijian, String jingdianzhutu, String jingdianjieshao, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 5);
		
		
		if(jingdianmingcheng==null||jingdianmingcheng.equals("")){pmap.put("jingdianmingcheng", null);}else{pmap.put("jingdianmingcheng", jingdianmingcheng);}
		if(jingdiandengji==null||jingdiandengji.equals("")){pmap.put("jingdiandengji", null);}else{pmap.put("jingdiandengji", jingdiandengji);}
		if(suozaichengshi==null||suozaichengshi.equals("")){pmap.put("suozaichengshi", null);}else{pmap.put("suozaichengshi", suozaichengshi);}
		if(diliweizhi==null||diliweizhi.equals("")){pmap.put("diliweizhi", null);}else{pmap.put("diliweizhi", diliweizhi);}
		if(menpiaojiage1==null||menpiaojiage1.equals("")){pmap.put("menpiaojiage1", null);}else{pmap.put("menpiaojiage1", menpiaojiage1);}
		if(menpiaojiage2==null||menpiaojiage2.equals("")){pmap.put("menpiaojiage2", null);}else{pmap.put("menpiaojiage2", menpiaojiage2);}
		if(kaifangshijian==null||kaifangshijian.equals("")){pmap.put("kaifangshijian", null);}else{pmap.put("kaifangshijian", kaifangshijian);}
		if(jingdianzhutu==null||jingdianzhutu.equals("")){pmap.put("jingdianzhutu", null);}else{pmap.put("jingdianzhutu", jingdianzhutu);}
		if(jingdianjieshao==null||jingdianjieshao.equals("")){pmap.put("jingdianjieshao", null);}else{pmap.put("jingdianjieshao", jingdianjieshao);}
		
		int total=lvyoujingdianService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyoujingdian> list=lvyoujingdianService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyoujingdian_list";
	}
	
	
	
	@RequestMapping("lyjdList.do")
	public String lyjdList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyoujingdian lvyoujingdian, String jingdianmingcheng, String jingdiandengji, String suozaichengshi, String diliweizhi, String menpiaojiage1,String menpiaojiage2, String kaifangshijian, String jingdianzhutu, String jingdianjieshao, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 5);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 5);
		
		
		if(jingdianmingcheng==null||jingdianmingcheng.equals("")){pmap.put("jingdianmingcheng", null);}else{pmap.put("jingdianmingcheng", jingdianmingcheng);}
		if(jingdiandengji==null||jingdiandengji.equals("")){pmap.put("jingdiandengji", null);}else{pmap.put("jingdiandengji", jingdiandengji);}
		if(suozaichengshi==null||suozaichengshi.equals("")){pmap.put("suozaichengshi", null);}else{pmap.put("suozaichengshi", suozaichengshi);}
		if(diliweizhi==null||diliweizhi.equals("")){pmap.put("diliweizhi", null);}else{pmap.put("diliweizhi", diliweizhi);}
		if(menpiaojiage1==null||menpiaojiage1.equals("")){pmap.put("menpiaojiage1", null);}else{pmap.put("menpiaojiage1", menpiaojiage1);}
		if(menpiaojiage2==null||menpiaojiage2.equals("")){pmap.put("menpiaojiage2", null);}else{pmap.put("menpiaojiage2", menpiaojiage2);}
		if(kaifangshijian==null||kaifangshijian.equals("")){pmap.put("kaifangshijian", null);}else{pmap.put("kaifangshijian", kaifangshijian);}
		if(jingdianzhutu==null||jingdianzhutu.equals("")){pmap.put("jingdianzhutu", null);}else{pmap.put("jingdianzhutu", jingdianzhutu);}
		if(jingdianjieshao==null||jingdianjieshao.equals("")){pmap.put("jingdianjieshao", null);}else{pmap.put("jingdianjieshao", jingdianjieshao);}
		
		int total=lvyoujingdianService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyoujingdian> list=lvyoujingdianService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyoujingdianlist";
	}
	
	@RequestMapping("lyjdListtp.do")
	public String lyjdListtp(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Lvyoujingdian lvyoujingdian, String jingdianmingcheng, String jingdiandengji, String suozaichengshi, String diliweizhi, String menpiaojiage1,String menpiaojiage2, String kaifangshijian, String jingdianzhutu, String jingdianjieshao, String issh){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 12);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 12);
		
		
		if(jingdianmingcheng==null||jingdianmingcheng.equals("")){pmap.put("jingdianmingcheng", null);}else{pmap.put("jingdianmingcheng", jingdianmingcheng);}
		if(jingdiandengji==null||jingdiandengji.equals("")){pmap.put("jingdiandengji", null);}else{pmap.put("jingdiandengji", jingdiandengji);}
		if(suozaichengshi==null||suozaichengshi.equals("")){pmap.put("suozaichengshi", null);}else{pmap.put("suozaichengshi", suozaichengshi);}
		if(diliweizhi==null||diliweizhi.equals("")){pmap.put("diliweizhi", null);}else{pmap.put("diliweizhi", diliweizhi);}
		if(menpiaojiage1==null||menpiaojiage1.equals("")){pmap.put("menpiaojiage1", null);}else{pmap.put("menpiaojiage1", menpiaojiage1);}
		if(menpiaojiage2==null||menpiaojiage2.equals("")){pmap.put("menpiaojiage2", null);}else{pmap.put("menpiaojiage2", menpiaojiage2);}
		if(kaifangshijian==null||kaifangshijian.equals("")){pmap.put("kaifangshijian", null);}else{pmap.put("kaifangshijian", kaifangshijian);}
		if(jingdianzhutu==null||jingdianzhutu.equals("")){pmap.put("jingdianzhutu", null);}else{pmap.put("jingdianzhutu", jingdianzhutu);}
		if(jingdianjieshao==null||jingdianjieshao.equals("")){pmap.put("jingdianjieshao", null);}else{pmap.put("jingdianjieshao", jingdianjieshao);}
		
		int total=lvyoujingdianService.getCount(pmap);
		pageBean.setTotal(total);
		List<Lvyoujingdian> list=lvyoujingdianService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "lvyoujingdianlisttp";
	}
	
	@RequestMapping("deleteLvyoujingdian.do")
	public String deleteLvyoujingdian(int id,HttpServletRequest request){
		lvyoujingdianService.delete(id);
		String url = request.getHeader("Referer");
		return "redirect:"+url;
		//return "redirect:lvyoujingdianList.do";
	}
	
	@RequestMapping("quchongLvyoujingdian.do")
	public void quchongLvyoujingdian(Lvyoujingdian lvyoujingdian,HttpServletResponse response){
		   Map<String,Object> map=new HashMap<String,Object>();
		   map.put("jingdianmingcheng", lvyoujingdian.getJingdianmingcheng());
		   System.out.println("jingdianmingcheng==="+lvyoujingdian.getJingdianmingcheng());
		   System.out.println("jingdianmingcheng222==="+lvyoujingdianService.quchongLvyoujingdian(map));
		   JSONObject obj=new JSONObject();
		   if(lvyoujingdianService.quchongLvyoujingdian(map)!=null){
				 obj.put("info", "ng");
			   }else{
				   obj.put("info", "景点名称可以用！");
				  
			   }
		   response.setContentType("text/html;charset=utf-8");
		   PrintWriter out=null;
		   try {
			out=response.getWriter();
			out.print(obj);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}
}
